package com.ufida.ids.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hereway.core.context.PageBean;
import com.hereway.core.dao.IBaseDAO;
import com.hereway.core.exception.ServiceException;
import com.hereway.core.service.BaseServiceImpl;
import com.ufida.ids.dao.IStaffDAO;
import com.ufida.ids.model.StaffModel;
import com.ufida.ids.service.IStaffService;
import com.ufida.ids.util.Constants;
import com.ufida.ids.util.DateUtil;
import com.ufida.ids.util.ExtUtil;

@Service("staffService")
public class StaffServiceImpl extends BaseServiceImpl<StaffModel,Long> implements IStaffService {
	
	private static Logger logger = Logger.getLogger(StaffServiceImpl.class);

	@Autowired
	IStaffDAO staffDAO;
	
	@Override
	protected IBaseDAO<StaffModel, Long> getDAO() {
		return staffDAO;
	}
	
	@Override
	public List<StaffModel> findByOrgId(Long orgId) throws ServiceException{
		return staffDAO.findByOrgId(orgId);
	}
	
	public String getStaffListByConPagging(String currentOrgId, String loginName, String staffName, String email, int pageNum) throws ServiceException{
		//分页信息
		PageBean page = new PageBean();
		page.setIndex(pageNum);
		
		String whereStr = getConStr(currentOrgId, loginName, staffName, email);
		//查询条目
		List<StaffModel> l = staffDAO.find(whereStr, "order by createTs desc", page);
		//查询条目数
		int count = staffDAO.findCountByCon(whereStr);
		//格式化日期
		for(StaffModel staff: l){
			staff.setCreateTsStr(DateUtil.getDateStrPattern1(staff.getCreateTs()));
			if(null != staff.getBirthday()){
				staff.setBirthdayStr(DateUtil.getDateStrPattern2(staff.getBirthday()));
			}
		}
		String retStr = ExtUtil.getJosonStr(l,new String[]{"birthday","userMappingModels", "postStaffModels"},count);
		return retStr;
	}

	public void update(StaffModel staffModel)throws ServiceException{
		staffDAO.update(staffModel);
	}
	
	public List<StaffModel> find(String idStr)throws ServiceException{
		String whereStr = " where 1 = 1 and staffId in"+ idStr;
		return staffDAO.find(whereStr);
	}

	private static String getConStr(String currentOrgId, String loginName, String staffName, String email){
		StringBuffer whereStr = new StringBuffer("where 1=1 "); 
		if(!"".equals(currentOrgId)){
			whereStr.append(" and orgId = ").append(currentOrgId);
		}
		
		if(!"".equals(loginName)){
			whereStr.append(" and loginName like '%").append(loginName).append("%'");
		}
		
		if(!"".equals(staffName)){
			whereStr.append(" and staffName like '%").append(staffName).append("%'");
		}
		
		if(!"".equals(email)){
			whereStr.append(" and email like '%").append(email).append("%'");
		}
		
		whereStr.append(" and entityStatus != ").append(Constants.STATUS_DEL);
		
		return whereStr.toString();
	}
	
	
	
	@Override
	public StaffModel getByCode(String staffCode) throws ServiceException {
		List<StaffModel> list = staffDAO.find("where staffCode = '" + staffCode + "'");
		if(list != null)
			return list.get(0);
		
		return null;
	}
}
